package com.yiqi.lottery.feature.lottery.view;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.entity.bo.PageData;
import com.suozhang.framework.framework.BaseActivity;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.util.DateTool;
import com.yiqi.lottery.entity.bo.CouprceBo;
import com.yiqi.lottery.feature.lottery.contract.HallContract;
import com.yiqi.lottery.feature.lottery.dependencies.hall.DaggerHallComponent;
import com.yiqi.lottery.feature.lottery.dependencies.hall.HallPresenterModule;
import com.yiqi.lottery.feature.lottery.view.adapter.TableAdpter;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.yiqi.lottery.feature.lottery.view.adapter.BuyHallAdapter.txfloat;

public class BuyHallDetailActivity extends BaseActivity implements HallContract.View {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_lottery_hall_detail_publishman)
    TextView mTvLotteryHallDetailPublishman;
    @BindView(R.id.tv_lottery_hall_detail_date)
    TextView mTvLotteryHallDetailDate;
    @BindView(R.id.tv_lottery_hall_detail_ticheng)
    TextView mTvLotteryHallDetailTicheng;
    @BindView(R.id.tv_lottery_hall_detail_rengoucount)
    TextView mTvLotteryHallDetailRengoucount;
    @BindView(R.id.tv_lottery_hall_detail_fqrrengoucount)
    TextView mTvLotteryHallDetailFqrrengoucount;
    @BindView(R.id.tv_lottery_hall_detail_fqrbaodicount)
    TextView mTvLotteryHallDetailFqrbaodicount;
    @BindView(R.id.tv_lottery_hall_detail_xuanyan)
    TextView mTvLotteryHallDetailXuanyan;
    @BindView(R.id.tv_lottery_hall_detail_description)
    TextView mTvLotteryHallDetailDescription;
    @BindView(R.id.tv_lottery_hall_detail_count)
    TextView mTvLotteryHallDetailCount;
    @BindView(R.id.rv_table)
    RecyclerView mRvTable;
    @BindView(R.id.qyrgs)
    EditText mEdtInputBet;
    @BindView(R.id.tv_lottery_hall_detail_type)
    TextView mTvLotteryHallDetailType;
    @BindView(R.id.btn_lotto_hemai_chedan)
    TextView mBtnLottoHemaiChedan;
    @BindView(R.id.qyrg)
    TextView mQyrg;
    @BindView(R.id.btn_lotto_hemai_pay)
    TextView mBtnLottoHemaiPay;
    @BindView(R.id.tv_lottery_hall_detail_amount)
    TextView mTvLotteryHallDetailAmount;
    @BindView(R.id.view_bottom1)
    RelativeLayout mViewBottom1;
    @BindView(R.id.view_bottom2)
    RelativeLayout mViewBottom2;
    @BindView(R.id.tv_wancdu)
    TextView mTvWancdu;
    @BindView(R.id.tv_meifdenjia)
    TextView mTvMeifdenjia;
    @BindView(R.id.tv_shenyu)
    TextView mTvShenyu;
    @BindView(R.id.tv_shenyufs)
    TextView mTvShenyufs;

    private TableAdpter mAdapter;
    @Inject
    HallContract.Presenter mPresenter;

    CouprceBo mCouprceData = null;
    int mNum = 0;

    private int perPrize;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_buy_hall_detail;
    }

    @Override
    protected void initInjector() {
        DaggerHallComponent.builder().hallPresenterModule(new HallPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "合买详情", true, true);
        initAdapter();
    }

    @Override
    protected void initData() {

        int id = this.getIntent().getIntExtra("id", 0);
        boolean isshow = this.getIntent().getBooleanExtra("type", false);
        if (isshow) {
            mViewBottom1.setVisibility(View.VISIBLE);
            mViewBottom2.setVisibility(View.VISIBLE);
            mBtnLottoHemaiChedan.setVisibility(View.GONE);
        } else {
            mViewBottom1.setVisibility(View.GONE);
            mViewBottom2.setVisibility(View.GONE);
            mBtnLottoHemaiChedan.setVisibility(View.VISIBLE);
        }
        mPresenter.getHallDetail(id + "");
    }

    @Override
    protected void initEvent() {
        mEdtInputBet.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    if (!TextUtils.isEmpty(s.toString())) {
                        mNum = Integer.valueOf(s.toString());
                        mTvLotteryHallDetailCount.setText(Html.fromHtml("金额支付：<font color=\"#383838\">" + mNum + "份*" + perPrize + "元=" + mNum * perPrize + "元</font>"));
                    }
                } catch (Exception e) {

                }
            }
        });
    }

    private void initAdapter() {

        mAdapter = new TableAdpter();
        mRvTable.setLayoutManager(new LinearLayoutManager(this));
        mRvTable.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight((int) 0.2));
        mAdapter.bindToRecyclerView(mRvTable);
        LayoutInflater mInflater = LayoutInflater.from(this);
        LinearLayout contentView = (LinearLayout) mInflater.inflate(R.layout.item_lottery_hall_title, null);
        mAdapter.addHeaderView(contentView);
    }


    @Override
    public void showHallListSuccess(PageData<CouprceBo> hallBoPageData) {

    }

    @Override
    public void showHallListMoreSuccess(PageData<CouprceBo> hallBoPageData) {

    }

    @Override
    public void showHallListMoreError(String ex) {

    }


    @Override
    public void showHallListEmpty() {

    }

    @Override
    public void showHallDetailSuccess(CouprceBo selectItem) {
        mCouprceData = selectItem;
        String num = txfloat(selectItem.getPre_num(), selectItem.getShare());
        mTvWancdu.setText(num + "%");
        perPrize = (selectItem.getAmount_fee() / selectItem.getShare());
        mTvMeifdenjia.setText(perPrize + "");
        mTvShenyufs.setText("剩余" + (selectItem.getShare() - selectItem.getPre_num()) + "份");
        mTvShenyu.setText(selectItem.getShare() - selectItem.getPre_num() + "");
        mTvLotteryHallDetailPublishman.setText(Html.fromHtml("发布人名：<font color=\"#383838\">" + selectItem.getUser_info().getUsername() + "</font>"));
        mTvLotteryHallDetailDate.setText(Html.fromHtml("截止日期：<font color=\"#383838\">" + DateTool.TimeStamp2Date(selectItem.getFinish_time()) + "</font>"));
        mTvLotteryHallDetailTicheng.setText(Html.fromHtml("方案提成：<font color=\"#383838\">" + ((int) selectItem.getPercentage() + 1) + "%</font>"));
        mTvLotteryHallDetailRengoucount.setText(Html.fromHtml("已经认购：<font color=\"#383838\">" + selectItem.getPre_num() + "份</font>"));
        mTvLotteryHallDetailFqrrengoucount.setText(Html.fromHtml("发起人认购份数：<font color=\"#383838\">" + selectItem.getSubscribe() + "份</font>"));
        mTvLotteryHallDetailFqrbaodicount.setText(Html.fromHtml("发起人保底份数：<font color=\"#383838\">" + selectItem.getGuarantee() + "份</font>"));
        mTvLotteryHallDetailXuanyan.setText(Html.fromHtml("方案宣言：<font color=\"#383838\">" + selectItem.getDeclaration() + "</font>"));
        mTvLotteryHallDetailDescription.setText(Html.fromHtml("方案描述：<font color=\"#" +
                "" +
                "" +
                "\">" + selectItem.getDesc() + "</font>"));
        mTvLotteryHallDetailCount.setText(Html.fromHtml("金额支付：<font color=\"#383838\">" + mNum + "份*" + perPrize + "元=" + mNum * perPrize + "元</font>"));
        mTvLotteryHallDetailType.setText(selectItem.getBet_label());
        mTvLotteryHallDetailAmount.setText(selectItem.getAmount_fee() + "");
        mAdapter.setNewData(selectItem.getBet_plan());
    }

    @Override
    public void showCopurchSubmitSuccess(String msg) {
        showMsg("提交成功！");
        finish();
    }

    @Override
    public void showCopurchPaySuccess(String msg) {
        showMsg("获取参数成功！");
    }

    @Override
    public void showCancleSuccess() {
        showMsg("撤单成功！");
        finish();

    }

    @Override
    public void showCancleError(String ex) {
        showMsg(ex);
    }

    @Override
    public void showHallListError(String ex) {
        showErrorMsg(ex);
    }

    @OnClick({R.id.btn_lotto_hemai_chedan, R.id.btn_lotto_hemai_pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_lotto_hemai_chedan:
                if (mCouprceData != null) {
                    mPresenter.cancleCopurch(mCouprceData.getCopurce_id()+"");
                }
                break;
            case R.id.btn_lotto_hemai_pay:
                if (mNum <= 0) {
                    showMsg("请输入合买份数");
                    return;
                }
                if (mNum > (mCouprceData.getShare() - mCouprceData.getPre_num())) {
                    showMsg("购买分数不能大于剩余份数！");
                    return;
                }
                mPresenter.copurchSubmit(mCouprceData.getCopurce_id(), mNum);
                break;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
